const app = getApp();
Page({

  data: {
    name:'',
    use_month:'',
    level:'请选择掌握程度',
    levels: [
      {
        value: 1,
        name: '一般'
      },
      {
        value: 2,
        name: '熟练'
      },
      {
        value: 3,
        name: '良好'
      },
      {
        value: 4,
        name: '精通'
      }
    ],
    level_value:'',
    level_index:0,
    click_button:0
  },

  onLoad: function (options) {
    var that = this;
    that.setData({
      id: options.id
    })
    that.getKnowledgeInfo();
  },

  onShow: function () {

  },

  getKnowledgeInfo: function(e) {
    var that = this;
    wx.request({
      url: app.globalData.domain + 'index/getKnowledgeInfo',
      method: "POST",
      data: {
        id: that.data.id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: (res) => {
        that.setData({
          name:res.data.info.name,
          use_month:res.data.info.use_month,
          level_value:res.data.info.level,
        })
        if(res.data.info.level == 1){
          that.setData({
            level:'一般',
          }) 
        }else if(res.data.info.level == 2){
          that.setData({
            level:'熟练',
          }) 
        }else if(res.data.info.level == 3){
          that.setData({
            level:'良好',
          }) 
        }else if(res.data.info.level == 4){
          that.setData({
            level:'精通',
          }) 
        }else{
          that.setData({
            level:'请选择掌握程度',
          }) 
        }
      },
    })
  },

  get_name: function(e){
    var that=this;
    that.setData({
      name:e.detail.value
    })
  },

  get_use_month: function(e){
    var that=this;
    that.setData({
      use_month:e.detail.value
    })
  },

  bindPickerChange: function(e) {
    var that = this;
    if(e.detail.value=='请选择掌握程度'){
      that.setData({
        level: '请选择掌握程度'
      })
    }else{
      that.setData({
        level : that.data.levels[e.detail.value].name,
        level_value : that.data.levels[e.detail.value].value
      })
    }
  },

  submit_result:function(){
    var that = this;
    if (that.data.name == '') {
      wx.showToast({
        title: '请输入技能名称',
        icon: 'none',
        duration: 2000,
      })
    } else if (that.data.use_month  == '') {
      wx.showToast({
        title: '请输入使用时长(月)',
        icon: 'none',
        duration: 2000,
      })
    } else if (that.data.level=='请选择掌握程度') {
      wx.showToast({
        title: '请选择掌握程度',
        icon: 'none',
        duration: 2000,
      })
    } else {
      this.setData({
        click_button: 1
      })
      wx.request({
        url: app.globalData.domain + 'index/updateKnowledge',
        method: "POST",
        data: {
          id: that.data.id,
          name: that.data.name,
          use_month: that.data.use_month,
          level: that.data.level_value,
          user_id: wx.getStorageSync('userinfo').id
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function(res) {
          if (res.data.code == 0) {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000,
              success: function() {
                that.setData({
                  name:'',
                  use_month:'',
                  level:'请选择掌握程度',
                  level_value:'',
                  click_button:0
                })
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1
                  });
                }, 1000);
              }
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000,
              success: function() {
                that.setData({
                  name:'',
                  use_month:'',
                  level:'请选择掌握程度',
                  level_value:'',
                  click_button:0
                })
              }
            })
          }
        }
      })
    }
  },

})